/******************************************************************
 *
 *
 *    Copyright (c) 2017-2027 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.theyeasy.dvmini.service.impl
 *
 *    Filename:    ProjectServiceImpl.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2017-2027
 *
 *    Company:     Theyeasy Telemedia Co.,Ltd
 *
 *    @author:     hm
 *
 *    @version:    1.0.0
 *
 *    Create at:   2017年10月22日 下午5:53:36
 *
 *    Revision:
 *
 *    2017年10月22日 下午5:53:36
 *        - first revision
 *
 *****************************************************************/
package com.theyeasy.dvmini.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.testng.collections.Maps;
import org.testng.internal.annotations.Sets;

import com.beust.jcommander.internal.Lists;
import com.theyeasy.dvmini.dao.ChatlogDao;
import com.theyeasy.dvmini.model.Chatlog;
import com.theyeasy.dvmini.model.Sales;
import com.theyeasy.dvmini.model.WebSockMsg;
import com.theyeasy.dvmini.service.ChatLogService;
import com.theyeasy.dvmini.service.DvminiSocketService;
import com.theyeasy.dvmini.service.SalesService;
import com.theyeasy.dvmini.util.PageResults;
import com.theyeasy.dvmini.vo.ChatLog_DistinctFansIdVO;
import com.theyeasy.dvmini.vo.Chatlog_Vo;
import com.theyeasy.dvmini.vo.Vo_msg;

import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName ProjectServiceImpl
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author hm
 * @Date 2017年10月22日 下午5:53:36
 * @version 1.0.0
 */
@Service
public class ChatLogServiceImpl implements ChatLogService {

	@Autowired
	ChatlogDao chatlogDao;

	@Autowired
	DvminiSocketService dvminiSocketService;

	@Autowired
	SalesService salesService;

	@Override
	public PageResults<Chatlog_Vo> getChatLogList(Integer page, Integer replytype, String date1, String date2,
			String keyword) {
		List<Chatlog_Vo> list = chatlogDao.getChatlogList((page - 1) * 10, 10, replytype, date1, date2, keyword);
		Integer total = chatlogDao.getChatlogCount(replytype, date1, date2, keyword);
		PageResults<Chatlog_Vo> pageResults = new PageResults<>();
		pageResults.setPage(page);
		pageResults.setPageSize(10);
		pageResults.setRows(list);
		pageResults.setTotal(total);
		return pageResults;
	}

	@Override
	public int insertChatLog(Chatlog log) {
		log.setId(null);
		return chatlogDao.insertSelective(log);
	}

	@Override
	public void updateChatLogUnReaded(Integer... ids) {
		Example example = new Example(Chatlog.class);
		List<Integer> asList = Arrays.asList(ids);
		example.createCriteria().andIn("id", asList);
		Chatlog log = new Chatlog();
		log.setReaded(true);
		log.setReadTime(new Date());
		chatlogDao.updateByExampleSelective(log, example);
	}

	public Vo_msg getChatListDistinctFansIdIn(Integer salesId) {
		Sales sales = salesService.getSales(salesId);
		List<Integer> ids =Lists.newArrayList();
		ids.add(salesId);
		if (sales.getIsvanker()) {
			ids.add(0);
		}
		List<ChatLog_DistinctFansIdVO> chatListDistinctFansIdIn = chatlogDao.getChatListDistinctFansIdIn(ids,salesId);
		List<ChatLog_DistinctFansIdVO> data = Lists.newArrayList();
		Set<Integer> set = Sets.newHashSet();
		for (ChatLog_DistinctFansIdVO vo: chatListDistinctFansIdIn) {
			boolean contains = set.contains(vo.getFansid());
			if(!contains) {
				if(salesId.equals(vo.getSalesid())) {
					data.add(vo);
				}
				
				set.add(vo.getFansid());
			}
		}
		return new Vo_msg(0, data);
	}

	@Override
	public Vo_msg getChatLogListBySalesId(Integer fromId, Integer fansid, int page) {
		Set<Integer> fansIds = null;

		if (fansid == null || fansid <= 0) {
			return getChatListDistinctFansIdIn(fromId);
		} else {
			fansIds = Sets.newHashSet();
			fansIds.add(fansid);
		}

		Map<String, Object> result = Maps.newHashMap();
		Sales sales = salesService.getSales(fromId);
		List<Integer> ids =Lists.newArrayList();
		ids.add(fromId);
		if (sales.getIsvanker()) {
			ids.add(0);
		}
		List<Chatlog_Vo> chatLogs = Lists.newArrayList();
		for (Integer integer : fansIds) {
			List<Chatlog_Vo> logListBySalesId = chatlogDao.getChatLogListBySalesId((page - 1) * 50, 50, ids,
					integer);
			Integer chatlogCountBySalesId = chatlogDao.getChatlogCountBySalesId(fromId, integer);
			if (!logListBySalesId.isEmpty()) {
				chatLogs.addAll(logListBySalesId);
				result.put("data", chatLogs);
				result.put("count", chatlogCountBySalesId);
			}
		}

		return new Vo_msg(0, result);

	}

	@Override
	public void updateChatLogUnReaded(Integer salesId, Integer fansid) {
		Example example = new Example(Chatlog.class);
		example.createCriteria().andEqualTo("salesid", salesId).andEqualTo("fansid", fansid).andEqualTo("readed",
				false);
		Chatlog log = new Chatlog();
		log.setReaded(true);
		log.setReadTime(new Date());
		chatlogDao.updateByExampleSelective(log, example);
	}

}
